﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Common
{

    public enum AcquisitionType
    {
         WholeCycle=0,
    }

    public class Cls_ACQUnits : BaseNode
    {
        public Cls_ACQUnits(XmlNode Node)
            : base(Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
            _XmlNode = Node;
        }

        private List<Cls_ACQUnit> _list = null;

        public List<Cls_ACQUnit> ACQUnitList
        {
            get
            {
                Cls_ACQUnit c = null;
                if (null != _list)
                    _list.Clear();
                else
                    _list = new List<Cls_ACQUnit>();
                foreach (XmlNode x in _XmlNode.SelectNodes("ACQUnit"))
                {
                    c = new Cls_ACQUnit(x);
                    _list.Add(c);
                }
                _list.Sort();
                return _list;
            }
        }

        public Cls_ACQUnit ACQUnit(int id)
        {
            XmlNode Acq;
            XmlDocument doc = this._XmlNode.OwnerDocument;

            //add new 
            Acq = _XmlNode.SelectSingleNode("ACQUnit[@id=" + id.ToString() + "]");
            if (Acq == null) return null;

            return new Cls_ACQUnit(Acq);

        }

        //DeleteUnit
        //添加时间：2014/8/4
        //添加人：李福安
        //修改时间：
        //修改人：
        public void DeleteUnit(int id)
        {
            Cls_ACQUnit ch = this.ACQUnit(id);
            if (null == ch) return;

            XmlNode n = ch.Node.ParentNode;
            n.RemoveChild(ch.Node);
        }


        //Add 
        //添加时间：2014/8/1
        //添加人：洪伟刚
        //修改时间：2014/8/4
        //修改人：李福安
        public Cls_ACQUnit AddACQUnit(int id)
        {
            //find 
            XmlNode Acq;
            XmlDocument doc = this._XmlNode.OwnerDocument;
            Cls_ACQUnit ExistACQ = ACQUnit(id);
            if (null != ExistACQ) return ExistACQ;

            //add new 
            Acq = _XmlNode.AppendChild(doc.CreateElement("ACQUnit"));
            Acq.Attributes.Append(doc.CreateAttribute("id")).Value = id.ToString();

            //set property
            Acq.AppendChild(doc.CreateElement("Name")).InnerText = "采集单元" + (id+1);
            Acq.AppendChild(doc.CreateElement("UnitName")).InnerText = "采集单元" + (id+1);
            Acq.AppendChild(doc.CreateElement("Port")).InnerText = "3000";
            Acq.AppendChild(doc.CreateElement("IPAddress")).InnerText = Return_IPAddress();
            Acq.AppendChild(doc.CreateElement("MACAddress")).InnerText = "";
            Acq.AppendChild(doc.CreateElement("HostIPAddress")).InnerText = "127.0.0.1";
            Acq.AppendChild(doc.CreateElement("HostMACAddress")).InnerText = "";
            Acq.AppendChild(doc.CreateElement("DoubleFrequency")).InnerText = "64";
            Acq.AppendChild(doc.CreateElement("CyclesNumber")).InnerText = "16";
            Acq.AppendChild(doc.CreateElement("UIRefreshFrequency")).InnerText = "1";
            Acq.AppendChild(doc.CreateElement("SystemDebug")).InnerText = "";
            Acq.AppendChild(doc.CreateElement("LocalSave")).InnerText = "0";
            Acq.AppendChild(doc.CreateElement("AcquisitionType")).InnerText = "";
            Acq.AppendChild(doc.CreateElement("HostPort")).InnerText = Return_HostPort();

            Acq.AppendChild(doc.CreateElement("HourDBInterval")).InnerText = "2";
            Acq.AppendChild(doc.CreateElement("DayDBInterval")).InnerText = "1";
            Acq.AppendChild(doc.CreateElement("WeekDBInterval")).InnerText = "7";
            Acq.AppendChild(doc.CreateElement("MonthDBInterval")).InnerText = "30";
            Acq.AppendChild(doc.CreateElement("YearDBInterval")).InnerText = "6";
            Acq.AppendChild(doc.CreateElement("VariableLoadDB")).InnerText = "";
            Acq.AppendChild(doc.CreateElement("StartSpeed")).InnerText = "";
            Acq.AppendChild(doc.CreateElement("StopSpeed")).InnerText = "";
            Acq.AppendChild(doc.CreateElement("BlackboxBeforeEvent")).InnerText = "";
            Acq.AppendChild(doc.CreateElement("BlackboxAfterEvent")).InnerText = "";
            Acq.AppendChild(doc.CreateElement("GeneralChartBackImage")).InnerText = "";
            Acq.AppendChild(doc.CreateElement("SlowSignelType")).InnerText = "";
            return new Cls_ACQUnit (Acq);
        }

        //自动设置下位机IP的默认值
        public string Return_IPAddress()
        {

            string address ="";
            for (int i = 1; i <= 255;i++ )
            {
                address = "127.0.0." + i.ToString();
                bool yorn = true;
                foreach (Cls_ACQUnit acq in ACQUnitList)
                {
                    if (acq.IPAddress == address)
                    {
                        yorn = false;
                        break;
                    }
                }

                if (yorn)
                {
                    break;
                }
            }

            return address;
        }

   

        //自动设置上位机的端口
        public string Return_HostPort()
        {

            int maxport = 6006;
            foreach (Cls_ACQUnit acq in ACQUnitList)
            {
                if (acq.HostPort != null)
                {
                    int hport = acq.HostPort;
                    if (hport > maxport)
                    {
                        maxport = hport;
                    }
                }
            }
            maxport++;
            return maxport.ToString();
        }
    }






    public class Cls_ACQUnit:BaseNode
    {

       // public TcpIpControl MyTcpIpControl = null;

        public Cls_ACQUnit(XmlNode Node)
            : base(Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
            _XmlNode = Node;
           // MyTcpIpControl = new TcpIpControl(this);
        }

        public string IPAddress
        {
            get
            {
                return _XmlNode["IPAddress"].InnerText;
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["IPAddress"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("IPAddress"));
                _XmlNode["IPAddress"].InnerText = value.ToString();
            }
        }

        public int Port
        {
            get
            {
                if (_XmlNode == null || _XmlNode["Port"].InnerText == "")
                {
                    return 6008;
                }
                return int.Parse(_XmlNode["Port"].InnerText);
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["Port"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("Port"));
                _XmlNode["Port"].InnerText = value.ToString();
            }
        }



        public Cls_TestPointList TestPointList
        {
            get
            {
                XmlNode x = _XmlNode.SelectSingleNode("TestPointList");
                if (x == null)
                    x = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("TestPointList"));
                // return null;
                return new Cls_TestPointList(x);
            }
        }

        public string SlowSignelType
        {
            get
            {
                if (_XmlNode["SlowSignelType"] != null)
                {
                    return _XmlNode["SlowSignelType"].InnerText;
                }
                else 
                {
                    return "";
                }
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["SlowSignelType"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("SlowSignelType"));
                _XmlNode["SlowSignelType"].InnerText = value.ToString();
            }
        }
        public int HostPort
        {
            get
            {
                if (_XmlNode == null||_XmlNode["HostPort"].InnerText=="")
                {
                    return 6007;
                }
                return int.Parse(_XmlNode["HostPort"].InnerText);
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["HostPort"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("HostPort"));
                _XmlNode["HostPort"].InnerText = value.ToString();
            }
        }

        public string MACAddress
        {
            get
            {
                return _XmlNode["MACAddress"].InnerText;
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["MACAddress"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("MACAddress"));
                _XmlNode["MACAddress"].InnerText = value.ToString();
            }
        }

        public string HostIPAddress
        {
            get
            {
                return _XmlNode["HostIPAddress"].InnerText;
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["HostIPAddress"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("HostIPAddress"));
                _XmlNode["HostIPAddress"].InnerText = value.ToString();
            }
        }



        public string HostMACAddress
        {
            get
            {
                return _XmlNode["HostMACAddress"].InnerText;
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["HostMACAddress"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("HostMACAddress"));
                _XmlNode["HostMACAddress"].InnerText = value.ToString();
            }
        }

        public int DoubleFrequency
        {
            get
            {
                try
                {

                if (_XmlNode == null)
                {
                    return 0;
                }
                return int.Parse(_XmlNode["DoubleFrequency"].InnerText);

                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("DoubleFrequency");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("DoubleFrequency"));
                Node.InnerText = value.ToString();
            }
        }

        public int CyclesNumber
        {
            get
            {
                try
                {

              
                if (_XmlNode == null)
                {
                    return 0;
                }
                }
                catch (Exception)
                {

                    return 0;
                }
                return int.Parse(_XmlNode["CyclesNumber"].InnerText);
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("CyclesNumber");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("CyclesNumber"));
                Node.InnerText = value.ToString();
            }
        }

        public int UIRefreshFrequency
        {
            get
            {
                try
                {

           
                if (_XmlNode == null)
                {
                    return 0;
                }
                return int.Parse(_XmlNode["UIRefreshFrequency"].InnerText);
                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("UIRefreshFrequency");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("UIRefreshFrequency"));
                Node.InnerText = value.ToString();
            }
        }

        public int SystemDebug
        {
            get
            {
                try
                {

                if (_XmlNode == null || _XmlNode["SystemDebug"].InnerText=="")
                {
                    return 0;
                }
                return Convert.ToInt32(_XmlNode["SystemDebug"].InnerText);

                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("SystemDebug");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("SystemDebug"));
                Node.InnerText = value.ToString();
            }
        }

        public int LocalSave
        {
            get
            {
                try
                {

                if (_XmlNode == null || _XmlNode["LocalSave"].InnerText=="")
                {
                    return 0;
                }
                return Convert.ToInt32(_XmlNode["LocalSave"].InnerText);

                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("LocalSave");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("LocalSave"));
                Node.InnerText = value.ToString();
            }
        }

        public AcquisitionType AcquisitionType
        {
            get
            {
                return (AcquisitionType)Enum.Parse(typeof(AcquisitionType), _XmlNode["AcquisitionType"].InnerText, true);
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["AcquisitionType"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("AcquisitionType"));
                _XmlNode["AcquisitionType"].InnerText = value.ToString();
            }
        }

        public int HourDBInterval
        {
            get
            {
                try
                {

               
                if (_XmlNode == null)
                {
                    return 0;
                }
                return int.Parse(_XmlNode["HourDBInterval"].InnerText);
                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("HourDBInterval");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("HourDBInterval"));
                Node.InnerText = value.ToString();
            }
        }

        public int DayDBInterval
        {
            get
            {
                try
                {

            
                if (_XmlNode == null)
                {
                    return 0;
                }
                return int.Parse(_XmlNode["DayDBInterval"].InnerText);
                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("DayDBInterval");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("DayDBInterval"));
                Node.InnerText = value.ToString();
            }
        }

        public int WeekDBInterval
        {
            get
            {
                try
                {

             
                if (_XmlNode == null)
                {
                    return 0;
                }
                return int.Parse(_XmlNode["WeekDBInterval"].InnerText);
                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("WeekDBInterval");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("WeekDBInterval"));
                Node.InnerText = value.ToString();
            }
        }

        public int MonthDBInterval
        {
            get
            {
                try
                {

          
                if (_XmlNode == null)
                {
                    return 0;
                }
                return int.Parse(_XmlNode["MonthDBInterval"].InnerText);
                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("MonthDBInterval");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("MonthDBInterval"));
                Node.InnerText = value.ToString();
            }
        }

        public int YearDBInterval
        {
            get
            {
                try
                {

           
                if (_XmlNode == null)
                {
                    return 0;
                }
                return int.Parse(_XmlNode["YearDBInterval"].InnerText);
                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("YearDBInterval");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("YearDBInterval"));
                Node.InnerText = value.ToString();
            }
        }

        public int VariableLoadDB
        {
            get
            {
                try
                {

           
                if (_XmlNode == null)
                {
                    return 0;
                }
                return int.Parse(_XmlNode["VariableLoadDB"].InnerText);
                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("VariableLoadDB");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("VariableLoadDB"));
                Node.InnerText = value.ToString();
            }
        }

        public int StartSpeed
        {
   
            get
            {
                try
                {

                if (_XmlNode == null)
                {
                    return 0;
                }
                return int.Parse(_XmlNode["StartSpeed"].InnerText);

                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("StartSpeed");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("StartSpeed"));
                Node.InnerText = value.ToString();
            }

        }

        public int StopSpeed
        {
            get
            {
                try
                {
                    if (_XmlNode == null)
                    {
                        return 0;
                    }
                    return int.Parse(_XmlNode["StopSpeed"].InnerText);
                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("StopSpeed");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("StopSpeed"));
                Node.InnerText = value.ToString();
            }
        }

        public int BlackboxBeforeEvent
        {
            get
            {
                try
                {

                if (_XmlNode == null)
                {
                    return 0;
                }
                return int.Parse(_XmlNode["BlackboxBeforeEvent"].InnerText);

                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("BlackboxBeforeEvent");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("BlackboxBeforeEvent"));
                Node.InnerText = value.ToString();
            }
        }

        public int BlackboxAfterEvent
        {
            get
            {
                try
                {

            
                if (_XmlNode == null)
                {
                    return 0;
                }
                return int.Parse(_XmlNode["BlackboxAfterEvent"].InnerText);
                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("BlackboxAfterEvent");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("BlackboxAfterEvent"));
                Node.InnerText = value.ToString();
            }
        }

        public Cls_GeneralChartBackImage GeneralChartBackImage
        {
            get
            {
                XmlNode x = _XmlNode.SelectSingleNode("GeneralChartBackImage");
                if (x == null)
                    x = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("GeneralChartBackImage"));
                // return null;
                return new Cls_GeneralChartBackImage(x);
            }
        }


        public Cls_ACQCards ACQCardList
        {
            get
            {
                XmlNode x = _XmlNode.SelectSingleNode("ACQCardList");
                if (x == null)
                    x = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("ACQCardList"));
                return new Cls_ACQCards(x);
            }
        }
 
    }
}
